/**
 * 详情对话框
 */
var StockInfoDlg = {
    data: {
        id: "",
        productId: "",
        supplierId: "",
        shopId: "",
        warehouseId: "",
        shelfId: "",
        saleUsableCount: "",
        saleFreezeCount: "",
        saleUsedCount: "",
        warehouseUsableCount: "",
        warehouseFreezeCount: "",
        warehouseUsedCount: "",
        version: "",
        createTime: "",
        updateTime: ""
    }
};

layui.use(['form', 'admin', 'ax', 'laydate', 'upload', 'formSelects'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;


    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/stock/detail?id=" + Feng.getUrlParam("id"));
    var result = ajax.start();
    form.val('stockForm', result.data);

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        data.field.productId = $('#productIdSelect option:selected').val();
        data.field.supplierId = $('#supplierIdSelect option:selected').val();
        data.field.shopId = $('#shopIdSelect option:selected').val();
        data.field.warehouseId = $('#warehouseIdSelect option:selected').val();
        data.field.shelfId = $('#shelfIdSelect option:selected').val();
        var ajax = new $ax(Feng.ctxPath + "/stock/editItem", function (data) {
            Feng.success("更新成功！");
            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);
            //关掉对话框
            admin.closeThisDialog();
        }, function (data) {
            Feng.error("更新失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });

    //商品
    $.ajax({
        url: '/baseCommonSelect/getProductList',
        dataType: 'json',
        type: 'post',
        success: function (data) {
            var option;
            if (data !== null) {
                var productId = $("#productId").val();
                $.each(data, function (index, item) {
                    if (!productId) {
                        option = new Option(item.value, item.id);
                    } else {
                        option = new Option(item.value, item.id);
                        // // 如果是之前的parentId则设置选中
                        if (item.id === productId) {
                            option.setAttribute("selected", 'true');
                        }
                    }
                    $('#productIdSelect').append(option);//往下拉菜单里添加元素
                    form.render("select");
                });
            } else {
                $("#productIdSelect").append(new Option("暂无数据", ""));
            }
            //重新渲染
        }
    });

    //供应商
    $.ajax({
        url: '/baseCommonSelect/getFactoryNumList',
        dataType: 'json',
        type: 'post',
        success: function (data) {
            var option;
            if (data !== null) {
                var supplierId = $("#supplierId").val();
                $.each(data, function (index, item) {
                    if (!supplierId) {
                        option = new Option(item.value, item.id);
                    } else {
                        option = new Option(item.value, item.id);
                        // // 如果是之前的parentId则设置选中
                        if (item.id === supplierId) {
                            option.setAttribute("selected", 'true');
                        }
                    }
                    $('#supplierIdSelect').append(option);//往下拉菜单里添加元素
                    form.render("select");
                });
            } else {
                $("#supplierIdSelect").append(new Option("暂无数据", ""));
            }
            //重新渲染
        }
    });

    //分店
    $.ajax({
        url: '/baseCommonSelect/getShopList',
        dataType: 'json',
        type: 'post',
        success: function (data) {
            var option;
            if (data !== null) {
                var shopId = $("#shopId").val();
                $.each(data, function (index, item) {
                    if (!shopId) {
                        option = new Option(item.value, item.id);
                    } else {
                        option = new Option(item.value, item.id);
                        // // 如果是之前的parentId则设置选中
                        if (item.id === shopId) {
                            option.setAttribute("selected", 'true');
                        }
                    }
                    $('#shopIdSelect').append(option);//往下拉菜单里添加元素
                    form.render("select");
                });
            } else {
                $("#shopIdSelect").append(new Option("暂无数据", ""));
            }
            //重新渲染
        }
    });

    //仓库
    $.ajax({
        url: '/baseCommonSelect/initWarehouseInfos',
        dataType: 'json',
        type: 'post',
        success: function (data) {
            var option;
            if (data !== null) {
                var warehouseId = $("#warehouseId").val();
                $.each(data, function (index, item) {
                    if (!warehouseId) {
                        option = new Option(item.value, item.id);
                    } else {
                        option = new Option(item.value, item.id);
                        // // 如果是之前的parentId则设置选中
                        if (item.id === warehouseId) {
                            option.setAttribute("selected", 'true');
                        }
                    }
                    $('#warehouseIdSelect').append(option);//往下拉菜单里添加元素
                    form.render("select");
                });
            } else {
                $("#warehouseIdSelect").append(new Option("暂无数据", ""));
            }
            //重新渲染
        }
    });

    var warehouseId = $('#warehouseId').val();
    //货架
    $.ajax({
        url: '/baseCommonSelect/initShelfSelectOptions',
        dataType: 'json',
        type: 'post',
        data: {warehouseId: warehouseId},
        success: function (data) {
            let option;
            if (data !== null) {
                var shelfId = $("#shelfId").val();
                $.each(data, function (index, item) {
                    if (!shelfId) {
                        option = new Option(item.value, item.id);
                    } else {
                        option = new Option(item.value, item.id);
                        // // 如果是之前的parentId则设置选中
                        if (item.id === shelfId) {
                            option.setAttribute("selected", 'true');
                        }
                    }
                    $('#shelfIdSelect').append(option);//往下拉菜单里添加元素
                    form.render("select");
                });
            } else {
                $("#shelfIdSelect").append(new Option("暂无数据", ""));
            }
            //重新渲染
        }
    });

    //监听仓库的下拉框事件，设置货架下拉框
    form.on('select(warehouseIdSelect)', function (data) {
        var warehouseId = data.value;
        $.ajax({
            url: '/baseCommonSelect/initShelfSelectOptions',
            dataType: 'json',
            data: {warehouseId: warehouseId},
            type: 'post',
            success: function (data) {
                if (data !== null) {
                    $("#shelfId").empty();
                    $("#shelfId").append(new Option());
                    $.each(data, function (index, item) {
                        $('#shelfId').append(new Option(item.value, item.id));
                    });
                } else {
                    $("#shelfId").append(new Option("暂无数据", ""));
                }
                //重新渲染
                form.render("select");
            }
        });

    });
});